package com.link.play.fit.ai.ui.guide

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.kv.AuthCache
import com.link.play.fit.ai.store.CloudStore
import com.link.play.fit.ai.store.CloudStore.DOC_USER_GUIDE
import com.link.play.fit.ai.utils.DateEx.calculateAge
import com.link.play.fit.ai.utils.DateEx.getTimestampFromDateTime
import com.link.play.fit.ai.utils.generateBMRFemale
import com.link.play.fit.ai.utils.generateBMRMale
import com.link.play.fit.ai.utils.getCarbsByCal
import com.link.play.fit.ai.utils.getFatsByCal
import com.link.play.fit.ai.utils.getProteinByCal
import com.link.play.fit.ai.utils.kgCal
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlin.math.max
import kotlin.math.roundToInt

class GuideViewModel : ViewModel() {

    private val TAG = "GuideViewModel"

    var userGuideState: MutableStateFlow<UserGuide> = MutableStateFlow(UserGuide())

    init {
        viewModelScope.launch {
            val userGuide = CloudStore.get<UserGuide>(DOC_USER_GUIDE)
            userGuide?.let { old ->
                userGuideState.update { old }
            }
        }
    }


    fun getUserAage() {
        val data = userGuideState.value
        val age =
            calculateAge(getTimestampFromDateTime(data.bornYear, data.bornMonth, data.bornDay, 1, 1))

        data.age = age
    }

    fun getBMR() {
        val data: UserGuide = userGuideState.value
        val exerciseRate = when (data.workouts) {
            0 -> 1.0
            1 -> 1.2
            2 -> 1.375
            else -> {
                1.0
            }
        }
        val bmr = if (data.gender == 0)
            (generateBMRMale(data.weightKg, data.heightCm, data.age) * exerciseRate).roundToInt()
        else
            (generateBMRFemale(data.weightKg, data.heightCm, data.age) * exerciseRate).roundToInt()
        data.bmr = bmr
    }

    /**
     * 计算每日目标值
     */
    fun customDailyPlan() {

        val data = userGuideState.value

        var caloriesDaily = 0
        var proteinDaily = 0
        var fatsDaily = 0
        var carbsDaily = 0

        val dailyCal = data.perWeightKg / 7f * kgCal


        val action = {
            val d = if (data.gender == 0) 1.0f else 0.75f
            proteinDaily = getProteinByCal(data.weightLbs, caloriesDaily, d)
            fatsDaily = getFatsByCal(caloriesDaily)
            carbsDaily = getCarbsByCal(caloriesDaily, proteinDaily, fatsDaily)
        }
        when (data.goal) {
            0 -> {
                caloriesDaily = data.bmr + dailyCal.roundToInt()
                action.invoke()
            }

            1 -> {
                caloriesDaily = data.bmr
                action.invoke()
            }

            2 -> {
                caloriesDaily = data.bmr - dailyCal.roundToInt()
                caloriesDaily = max(caloriesDaily, 1200)
                action.invoke()
            }
        }

        Log.e(
            TAG,
            "bmr=${data.bmr} " +
                    "dailyCal=$dailyCal " +
                    "caloriesDaily=$caloriesDaily " +
                    "proteinDaily=$proteinDaily " +
                    "fatsDaily=$fatsDaily " +
                    "carbsDaily=$carbsDaily"
        )

        data.caloriesDaily = caloriesDaily
        data.proteinDaily = proteinDaily
        data.fatsDaily = fatsDaily
        data.carbsDaily = carbsDaily
        data.dataReady = true

        Log.e(TAG, "data.dataReady = true")
        AuthCache.tempUserGuide = Gson().toJson(data)
    }

}